/*
 * @(#)SolrServiceImpl.java 2012-02-11
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.search.common.bean.CmsInformationBean;
import cn.shangjia.search.common.bean.SolrCompanyInfo;
import cn.shangjia.search.common.client.ISolrClient;
import cn.shangjia.search.common.client.SolrQueryParams;
import cn.shangjia.search.common.client.SolrResponse;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.SolrQueryService;

/**
 *
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @version 1.0, 2012-02-11 14:09:33
 * @since 1.0
 */
@Service
public class SolrQueryServiceImpl implements SolrQueryService{
	
	private static final Logger logger = ShopLoggerFactory.getLogger(SolrQueryService.class);
	
	@Resource
	private ISolrClient cmsInformatSolrClient;
	
	@Resource
	private ISolrClient companySolrClient;

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.SolrQueryService#findFriendLink(java.lang.String[])
	 */
	@Override
	public List<SolrCompanyInfo> findFriendLink(String... keywords) {
		try {
			String keyword = getKeyword(keywords);
			SolrQueryParams params = new SolrQueryParams();
			params.setPage(1);
			params.setPageSize(8);
			params.setKeyword(keyword);
			SolrResponse response = companySolrClient.query(params, SolrCompanyInfo.class);
			return response.getData();
		} catch(Exception e) {
			logger.error(e);
			return new ArrayList<SolrCompanyInfo>();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.service.SolrQueryService#findCmsInformation(java.lang.String[])
	 */
	@Override
	public List<CmsInformationBean> findCmsInformation(String ... keywords) {
		try {
			String keyword = getKeyword(keywords);
			SolrQueryParams params = new SolrQueryParams();
			params.setPage(1);
			params.setPageSize(10);
			params.setKeyword(keyword);
			SolrResponse response = cmsInformatSolrClient.query(params, CmsInformationBean.class);
			return response.getData();
		} catch(Exception e) {
			logger.error(e);
			return new ArrayList<CmsInformationBean>();
		}
		
	}
	
	private String getKeyword(String ... keywords) {
		String keyword = "";
		if (keywords != null && keywords.length > 0) 
			for (String k : keywords) 
				keyword += ( k + " ");
		return keyword;
	}

}
